package aom.ju.ss.view.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.view.View
import aom.ju.ss.R
import aom.ju.ss.databinding.DaohangDialogBinding
import aom.ju.ss.utils.show

/**
 * 1
 * 导航弹窗
 */
class NavigationPopup(activity: Activity?, var lat: String?, var lon: String?) :
    BaseDialog(activity) {
    lateinit var binding: DaohangDialogBinding


    override fun init() {

        binding = vBinding as DaohangDialogBinding

        for (a in localMapList(activity)) {
            if (a == "com.baidu.BaiduMap") {
                binding.bd.visibility = View.VISIBLE
                binding.no.visibility = View.GONE
            }
            if (a == "com.autonavi.minimap") {
                binding.gd.visibility = View.VISIBLE
                binding.no.visibility = View.GONE
            }
            if (a == "com.tencent.map") {
                binding.tx.visibility = View.VISIBLE
                binding.no.visibility = View.GONE
            }
        }

        binding.bd.setOnClickListener {
            toBaidu(activity, lon, lat)
            dis()
        }

        binding.tx.setOnClickListener {
            toTencent(activity, lon, lat)
            dis()
        }
        binding.gd.setOnClickListener {
            toGaodeNavi(activity, lon, lat)
            dis()
        }
        binding.mapToastCancelbt.setOnClickListener {
            dis()
        }

        val list = localMapList(activity);
        if (list.size == 1) {
            when {
                list[0] == "com.baidu.BaiduMap" -> {
                    binding.bd.performClick()
                }
                list[0] == "com.autonavi.minimap" -> {
                    binding.gd.performClick()
                }
                list[0] == "com.tencent.map" -> {
                    binding.tx.performClick()
                }
                else -> {
                    "暂不支持此地图".show()
                }
            }
        }
    }


    override fun getLay(): Int {
        return R.layout.daohang_dialog
    }

    override fun isBottom(): Boolean {
        return true
    }

    init {
        setWidth(-1)

    }

    /**
     * 指定地图
     * 百度地图包名：com.baidu.BaiduMap
     *
     * 高德地图包名：com.autonavi.minimap
     *
     * 腾讯地图包名：com.tencent.map
     *
     * 谷歌地图 com.google.android.apps.maps
     *
     */
    fun initDataMapsList(): List<String> {
        val maps: MutableList<String> = ArrayList()
        maps.add("com.baidu.BaiduMap")
        maps.add("com.autonavi.minimap")
        maps.add("com.tencent.map")
        return maps
    }

    // 检索筛选后返回
    fun localMapList(context: Context): List<String> {
        val mapsList = initDataMapsList()
        val backList: MutableList<String> = ArrayList()
        for (i in mapsList.indices) {
            val avilible = isAvilible(context, mapsList[i])
            if (avilible) {
                backList.add(mapsList[i])
            }
        }
        return backList
    }

    // 百度地图
    fun toBaidu(context: Context, lon: String?, lat: String?) {
        val intent = Intent(
            "android.intent.action.VIEW",
            Uri.parse(
                "baidumap://map/geocoder?location=$lat,$lon"
            )
        )
        context.startActivity(intent)
    }

    // 高德地图
    fun toGaodeNavi(context: Context, lon: String?, lat: String?) {
        val intent = Intent(
            "android.intent.action.VIEW",
            Uri.parse(
                "androidamap://route?sourceApplication=appName&slat=&slon=&sname=起点&dlat=$lat&dlon=$lon&dname=目的地置&dev=0&t=2"
            )
        )
        context.startActivity(intent)
    }

    // 腾讯地图
    fun toTencent(context: Context, lon: String?, lat: String?) {
        val intent = Intent(
            "android.intent.action.VIEW",
            Uri.parse(
                "qqmap://map/routeplan?type=drive&from=&fromcoord=&to=目的地&tocoord=$lat,$lon&policy=0&referer=appName"
            )
        )
        context.startActivity(intent)
    }
}

fun isAvilible(context: Context, packageName: String): Boolean {
//获取packagemanager
    val packageManager: PackageManager = context.packageManager
    //获取所有已安装程序的包信息
    val packageInfos = packageManager.getInstalledPackages(0)
    //用于存储所有已安装程序的包名
    val packageNames: MutableList<String> = ArrayList()
    //从pinfo中将包名字逐一取出，压入pName list中
    if (packageInfos != null) {
        for (i in packageInfos.indices) {
            val packName = packageInfos[i].packageName
            packageNames.add(packName)
        }
    }
    //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
    return packageNames.contains(packageName)
}

